Attribute VB_Name = "MyENA"
'-----------------------------------------------------------------

Public Sub InitENA()
    
    Dim ENAName As String
    ENAName = SCPI.IEEE4882.IDN
    Dim SERIALNO As String
    SERIALNO = GetSn(ENAName)
    
    Dim vldtObj As ENAEncode
    Set vldtObj = New ENAEncode


    If (vldtObj.Validate(SERIALNO) = False) Then
        MsgBox "Invalid Licence, Copyright Needed......", vbOKOnly + vbQuestion, "Warning!"
        End
    Else
        SCPI.IEEE4882.RST
        SCPI.IEEE4882.CLS
    End If
    
End Sub

Public Sub SingleSweep(ch As Integer)
    SCPI.TRIGger.SEQuence.Source = "bus"
    SCPI.INITiate.CONTinuous = False
    SCPI.INITiate(ch).IMMediate
    SCPI.TRIGger.SEQuence.Single
    TempRes = SCPI.IEEE4882.OPC
End Sub

Public Sub CallState(Sta As String)
    SCPI.MMEMory.Load.state = Sta
    temp = SCPI.IEEE4882.OPC
    SCPI.DISPlay.Enable = False
End Sub
Public Function SetDisplayEnable(flag As Boolean)
    
    SCPI.DISPlay.Enable = flag
    
End Function
Public Function MeasMax(item As ItemConf)     'Measure max
    
    SCPI.CALCulate(item.ch).PARameter(item.Tr).Select
    SCPI.CALCulate(item.ch).SELected.MARKer(1).state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.Start = item.StartF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.STOP = item.StopF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "MAX"
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
    temp = SCPI.IEEE4882.OPC
    item.max = Val(Format(SCPI.CALCulate(item.ch).SELected.MARKer(1).y(0), "####.##"))
    
End Function

Public Function MeasMin(item As ItemConf)
    
    SCPI.CALCulate(item.ch).PARameter(item.Tr).Select
    SCPI.CALCulate(item.ch).SELected.MARKer(1).state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.Start = item.StartF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.STOP = item.StopF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "MIN"
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
    temp = SCPI.IEEE4882.OPC
    item.min = Val(Format(SCPI.CALCulate(item.ch).SELected.MARKer(1).y(0), "####.##"))
    
End Function

Public Function MeasRipple(item As ItemConf)
    temp = MeasMax(item)
    temp = MeasMin(item)
    item.max = item.max - item.min
    item.max = Val(Format(item.max, "####.##"))
End Function

Public Function MeasDelay(item As ItemConf)
    
    SCPI.CALCulate(item.ch).PARameter(item.Tr).Select
    SCPI.CALCulate(item.ch).SELected.Format = "GDELay"
    SCPI.CALCulate(item.ch).SELected.MARKer(1).state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.Start = item.StartF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.STOP = item.StopF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "MAX"
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
    temp = SCPI.IEEE4882.OPC
    item.max = Val(Format(SCPI.CALCulate(item.ch).SELected.MARKer(1).y(0) * 1000000000#, "####.##"))
    SCPI.CALCulate(item.ch).SELected.Format = "MLOG"
    
    'chang format back to dB
    
End Function

Public Function MeasDelayRipple(item As ItemConf)
    SCPI.CALCulate(item.ch).PARameter(item.Tr).Select
    SCPI.CALCulate(item.ch).SELected.Format = "GDELay"
    'Change format to nS
    SCPI.CALCulate(item.ch).SELected.MARKer(1).state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.Start = item.StartF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.STOP = item.StopF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "MAX"
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
    temp = SCPI.IEEE4882.OPC
    item.max = Val(Format(SCPI.CALCulate(item.ch).SELected.MARKer(1).y(0) * 1000000000#, "####.##"))
    
    SCPI.CALCulate(item.ch).PARameter(item.Tr).Select
    SCPI.CALCulate(item.ch).SELected.MARKer(1).state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.Start = item.StartF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.STOP = item.StopF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "MIN"
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
    temp = SCPI.IEEE4882.OPC
    item.min = Val(Format(SCPI.CALCulate(item.ch).SELected.MARKer(1).y(0) * 1000000000#, "####.##"))
    item.max = item.max - item.min
    
    SCPI.CALCulate(item.ch).SELected.Format = "MLOG"
    'chang format back to dB
    
End Function

Public Function SaveData(item As ItemConf)
    SCPI.CALCulate(item.ch).PARameter(item.Tr).Select
    item.TraceMem = SCPI.CALCulate(item.ch).SELected.DATA.SDATa
    temp = SCPI.IEEE4882.OPC
End Function
Public Function LoadToMem(SouItem As ItemConf, DesItem As ItemConf)
    SCPI.CALCulate(DesItem.ch).PARameter(DesItem.Tr).Select
    SCPI.CALCulate(DesItem.ch).SELected.DATA.SMEMory = SouItem.TraceMem
    temp = SCPI.IEEE4882.OPC
End Function
Public Function LoadToData(SouItem As ItemConf, DesItem As ItemConf)
    SCPI.CALCulate(DesItem.ch).PARameter(DesItem.Tr).Select
    SCPI.CALCulate(DesItem.ch).SELected.DATA.SDATa = SouItem.TraceMem
    temp = SCPI.IEEE4882.OPC
End Function
Public Function TraceDivMem(item As ItemConf)
    SCPI.CALCulate(item.ch).PARameter(item.Tr).Select
    SCPI.CALCulate(item.ch).SELected.Math.Function = "DIV"
    temp = SCPI.IEEE4882.OPC
    
    SCPI.CALCulate(item.ch).SELected.MARKer(1).state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.Start = item.StartF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.STOP = item.StopF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "MAX"
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
    temp = SCPI.IEEE4882.OPC
    item.max = Val(Format(SCPI.CALCulate(item.ch).SELected.MARKer(1).y(0), "####.##"))
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "MIN"
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
    temp = SCPI.IEEE4882.OPC
    item.min = Val(Format(SCPI.CALCulate(item.ch).SELected.MARKer(1).y(0), "####.##"))
    
    SCPI.CALCulate(item.ch).SELected.Math.Function = "NORM"
    
End Function
Public Function MeasVSWR(item As ItemConf)
    SCPI.CALCulate(item.ch).PARameter(item.Tr).Select
    SCPI.CALCulate(item.ch).SELected.Format = "SWR"
    SCPI.CALCulate(item.ch).SELected.MARKer(1).state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.Start = item.StartF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.STOP = item.StopF
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.DOMain.state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "MAX"
    SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
    temp = SCPI.IEEE4882.OPC
    item.max = Val(Format(SCPI.CALCulate(item.ch).SELected.MARKer(1).y(0), "#.##"))
    SCPI.CALCulate(item.ch).SELected.Format = "MLOG"
End Function
Public Function Meas1Db(item As ItemConf)
       Dim y_dat As Variant
       Dim MarkerTarget As Single
       Dim Data_Min As Single
       SCPI.CALCulate(item.ch).PARameter(item.Tr).Select
       SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "maximum"
       SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
       y_dat = SCPI.CALCulate(item.ch).SELected.MARKer(1).y
       MarkerTarget = y_dat(0)
       MarkerTarget = MarkerTarget - 1
       
       SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "minimum"
       SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
       y_dat = SCPI.CALCulate(item.ch).SELected.MARKer(1).y
       Data_Min = Val(y_dat(0)) 'read Min result
       If MarkerTarget > Data_Min Then
           SCPI.CALCulate(item.ch).PARameter(1).Select
           SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.TARGet = MarkerTarget
           SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "targ"
           SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
           item.min = SCPI.CALCulate(item.ch).SELected.MARKer(1).x
           item.min = Val(Format(item.min, "####.##"))
        Else
           MsgBox "Wrong 1dB measurement!", vbOKOnly + vbCritical, "Error!"
       End If
End Function
Public Function Meas1dbOut(item As ItemConf)
  Dim y_dat As Variant
       Dim MarkerTarget As Single
       Dim Data_Min As Single
       SCPI.CALCulate(item.ch).PARameter(item.Tr).Select
       SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "maximum"
       SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
       y_dat = SCPI.CALCulate(item.ch).SELected.MARKer(1).y
       MarkerTarget = y_dat(0)
       MarkerTarget = MarkerTarget - 1
       
       SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "minimum"
       SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
       y_dat = SCPI.CALCulate(item.ch).SELected.MARKer(1).y
       Data_Min = Val(y_dat(0)) 'read Min result
       If MarkerTarget > Data_Min Then
           SCPI.CALCulate(item.ch).PARameter(1).Select
           SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.TARGet = MarkerTarget
           SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.Type = "targ"
           SCPI.CALCulate(item.ch).SELected.MARKer(1).Function.EXECute
           item.min = SCPI.CALCulate(item.ch).SELected.MARKer(1).x + SCPI.CALCulate(item.ch).SELected.MARKer(1).y(0)
           item.min = Val(Format(item.min, "####.##"))
        Else
           MsgBox "Wrong 1dB measurement!", vbOKOnly + vbCritical, "Error!"
       End If
End Function

Public Function MeasPoint(item As ItemConf)     'Measure point
    SCPI.CALCulate(item.ch).PARameter(item.Tr).Select
    SCPI.CALCulate(item.ch).SELected.MARKer(1).state = True
    SCPI.CALCulate(item.ch).SELected.MARKer(1).x = item.StartF
    item.max = Val(Format(SCPI.CALCulate(item.ch).SELected.MARKer(1).y(0), "####.##"))
End Function

Public Function GetSn(DstName As String) As String
     GetSn = Mid(DstName, 29, 10)
End Function

Public Sub Preset()
        SCPI.SYSTem.Preset
End Sub
'add at 2013-06-07 by songchunming
Public Sub ChangeStartStop(item As ItemConf)
    
    Dim temp
    Dim flag As Boolean
    flag = SCPI.DISPlay.Enable
    SCPI.DISPlay.Enable = True
    SCPI.SENSe(item.ch).FREQuency.Start = item.StartF
    temp = SCPI.SENSe(item.ch).FREQuency.Start

    SCPI.SENSe(item.ch).FREQuency.STOP = item.StopF
    temp = SCPI.SENSe(item.ch).FREQuency.STOP
    SCPI.DISPlay.Enable = flag
    
End Sub
'add at 2013-06-28
Public Sub ClearError()
    
    Dim Err
    Err = SCPI.SYSTem.Error
    While (Err(0) <> 0)
        Err = SCPI.SYSTem.Error
    Wend
    
End Sub
'2013-07-18 by songchunming
'has Ecal?
Public Function ECalPath() As Boolean

    Dim path1, path2
    'SCPI.OUTPut.state = False
    path1 = SCPI.SENSe.CORRection.COLLect.ECAL.path(1)
    path2 = SCPI.SENSe.CORRection.COLLect.ECAL.path(2)
    If path1 = 0 Then
        ECalPath = False
        MsgBox "Path 1 is not connect!"
    ElseIf path2 = 0 Then
        ECalPath = False
        MsgBox "Path 2 is not connect!"
    'ElseIf path1 <> 1 Then
    '    ECalPath = False
    '    MsgBox "path1 must connect port1!"
    'ElseIf path2 <> 2 Then
    '    ECalPath = False
    '    MsgBox "path2 must connect port2!"
    Else
        ECalPath = True
    End If
    
End Function
'2013-07-20 by songchunming
'for old Version,example:L52 Martix
Public Sub ECalStartSingle()

    Dim Port(1) As Variant
    Port(0) = 1
    Port(1) = 2
    Dim temp As Integer
    temp = Frm_Main.HaveCh
    
    For i = 0 To temp - 1
        SCPI.SENSe(i + 1).CORRection.COLLect.ECAL.SOLT2 = Port
        temp = SCPI.IEEE4882.OPC
    Next i
    
End Sub
'2013-07-20 by songchunming
Public Sub ECalStartMulti(chs As Integer)
    
    Dim Port(1) As Variant
    Port(0) = 1
    Port(1) = 2
    SCPI.DISPlay.WINDow(chs).ACTivate
    SCPI.SENSe(chs).CORRection.COLLect.ECAL.SOLT2 = Port
    temp = SCPI.IEEE4882.OPC
    
End Sub
'Sub SetCh(ch() As Integer, stateCh As String)
'    temp = Split(stateCh, ",")
'    u = UBound(temp)
'    ReDim ch(u) As Integer
'    For i = 0 To u
'        ch(i) = CInt(temp(i))
'    Next i
'End Sub
Public Sub SaveState(path As String)

    SCPI.MMEMory.STORe.STYPe = "CST"
    SCPI.MMEMory.STORe.state = path
    temp = SCPI.IEEE4882.OPC
    
End Sub
'add at 2013-07-01 by songchunming
'Public Sub RFON()
'
'    SCPI.OUTPut.state = True
'
'End Sub
'add at 2013-07-01 by songchunming
'Public Sub RFOff()
'
'    SCPI.OUTPut.state = False
'
'End Sub
''add at 2013-07-04 by songchunming
'Public Sub SetTestState()
'
'    SCPI.TRIGger.SEQuence.Source = "BUS"
'    Dim temp As Integer
'    temp = Frm_Main.HaveCh
'    Dim ch() As Integer
'    ''Call SetCh(ch, temp)'
'
'    For i = 0 To UBound(ch)
'        SCPI.INITiate(ch(i)).CONTinuous = False
'    Next i
'
'
'End Sub
Public Sub ChsCommands(state As String, chs() As Integer, Command() As String, MeasPath() As String)
    
    Dim u As Integer, i As Integer, count As Integer
    u = UBound(DevConf())
    Dim list As New Collection
    Dim Cnt As New Collection
    
    For i = 0 To u
        If Len(DevConf(i).CalCh) > 0 And DevConf(i).Sta = state Then
            Call list.Add(DevConf(i).CalCh)
            Cnt.Add i
        End If
    Next i
    
    count = list.count
    If count > 0 Then
        ReDim chs(count - 1)
        ReDim Command(count - 1)
        ReDim MeasPath(count - 1)
        For i = 0 To count - 1
            temp = DevConf(Cnt.item(i + 1)).CalCh
            If temp = "" Then
                temp = 0
            End If
            chs(i) = CInt(temp)
            Command(i) = DevConf(Cnt(i + 1)).MatrixCommand
            MeasPath(i) = DevConf(Cnt(i + 1)).MeasPath
        Next i
    Else
        ReDim ch(0)
        ReDim Command(0)
        ReDim MeasPath(0)
        For i = 0 To u
            If Len(DevConf(i).MatrixCommand) > 0 And DevConf(i).Sta = state Then
                Command(0) = DevConf(i).MatrixCommand
                MeasPath(0) = DevConf(i).MeasPath
                Exit For
            End If
        Next i
    End If
    
End Sub

Public Function MeasMark(item As ItemConf)

    SCPI.CALCulate(item.ch).PARameter(item.Tr).Select
    'SCPI.CALCulate(item.ch).SELected.MARKer(item.StopF).y (0)
    temp = SCPI.IEEE4882.OPC
    item.min = Val(Format(SCPI.CALCulate(item.ch).SELected.MARKer(item.StopF).y(0), "####.##"))
    item.max = item.min

End Function
